package cn.desk.commons.util;

import java.util.LinkedList;
import java.util.List;

public class QueryCondition {
	private List<QueryCondition> queryConditions = new LinkedList<QueryCondition>();
	private String property;
	private Integer conditionType;
	public static final int CONDITION_TYPE_AND = 1;
	public static final int CONDITION_TYPE_OR = 2;
	public static final int CONDITION_TYPE_DESC = 4;
	public static final int CONDITION_TYPE_ASC = 8;
	public static final int CONDITION_TYPE_EQ = 16;
	public static final int CONDITION_TYPE_LLIKE = 32;
	public static final int CONDITION_TYPE_RLIKE = 64;
	public static final int CONDITION_TYPE_IS_NULL = 128;
	public static final int MATCH_TYPE_START = 1;

	public static final int MATCH_TYPE_END = 2;

	public static final int MATCH_TYPE_ANY = 4;

	private Object value;

	public static QueryCondition createEq(String property, Object value) {
		QueryCondition condition = new QueryCondition();
		condition.property = property;
		condition.value = value;
		condition.conditionType = CONDITION_TYPE_EQ;
		return condition;
	}

	public QueryCondition desc(String property) {
		QueryCondition con = new QueryCondition();
		con.setConditionType(CONDITION_TYPE_DESC);
		con.setProperty(property);
		queryConditions.add(con);
		return this;
	}

	public QueryCondition eq(String property, Object value) {
		return this;
	}

	public String getProperty() {
		return property;
	}

	public void setProperty(String property) {
		this.property = property;
	}

	public Object getValue() {
		return value;
	}

	public void setValue(Object value) {
		this.value = value;
	}

	public Integer getConditionType() {
		return conditionType;
	}

	public void setConditionType(Integer conditionType) {
		this.conditionType = conditionType;
	}

	public List<QueryCondition> getQueryConditions() {
		return queryConditions;
	}

	public void setQueryConditions(List<QueryCondition> queryConditions) {
		this.queryConditions = queryConditions;
	}

	public QueryCondition andEq(String property, Object value) {
		QueryCondition condition = new QueryCondition();
		condition.property = property;
		condition.value = value;
		condition.conditionType = CONDITION_TYPE_EQ | CONDITION_TYPE_AND;
		queryConditions.add(condition);
		return this;
	}

	public QueryCondition andLike(String property, String value,
			Integer matchType) {
		QueryCondition condition = new QueryCondition();
		condition.property = property;
		condition.value = value;
		switch (matchType) {
		case MATCH_TYPE_START:
			condition.conditionType = CONDITION_TYPE_LLIKE;
			break;
		case MATCH_TYPE_END:
			condition.conditionType = CONDITION_TYPE_RLIKE;
			break;
		case MATCH_TYPE_ANY:
			condition.conditionType = CONDITION_TYPE_RLIKE
					| CONDITION_TYPE_LLIKE;
			break;
		default:
			break;
		}
		queryConditions.add(condition);
		return this;
	}

	public QueryCondition andIsNull(String property) {
		QueryCondition condition = new QueryCondition();
		condition.property = property;
		condition.conditionType = CONDITION_TYPE_IS_NULL | CONDITION_TYPE_AND;
		queryConditions.add(condition);
		return this;
	}

	public QueryCondition asc(String string) {
		QueryCondition con = new QueryCondition();
		con.setConditionType(CONDITION_TYPE_ASC);
		con.setProperty(string);
		queryConditions.add(con);
		return this;

	}
}
